package com.tanhua.server.controller;

import com.tanhua.domain.vo.CommentVo;
import com.tanhua.domain.vo.MomentVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.server.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 针对评论表控制层
 */
@RestController
@RequestMapping("/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 评论列表
     * movementId:发布id
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity queryCommentListByPage(String movementId,@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int pagesize){
        PageResult<CommentVo> pageResult = commentService.queryCommentListByPage(movementId,page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 发表评论
     * movementId：发布id  comment：评论内容
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity postComment(@RequestBody Map<String,String> map){
        String movementId = map.get("movementId");//发布id
        String content = map.get("comment");//评论内容
        commentService.postComment(movementId,content);
        return ResponseEntity.ok(null);
    }



    /**
     * 评论点赞
     * id:评论id
     */
    @RequestMapping(value = "/{id}/like",method = RequestMethod.GET)
    public ResponseEntity like(@PathVariable("id") String commentId){
        Long likeCount = commentService.like(commentId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 评论取消点赞
     */
    @RequestMapping(value = "/{id}/dislike",method = RequestMethod.GET)
    public ResponseEntity dislike(@PathVariable("id") String commentId){
        Long likeCount = commentService.dislike(commentId);
        return ResponseEntity.ok(likeCount);
    }
}
